$(function () {
    Time();
    load();
})

let list = [];
function fh() {
    localStorage.removeItem("BianMde");
    location.href = "../../li_meng/SheJiDanBianGeng.html";
}

function Time() {
    var time = new Date();
    var y = time.getFullYear();
    var mon = time.getMonth() + 1; //0-11
    var d = time.getDate();
    var h = time.getHours();
    var m = time.getMinutes();
    var s = time.getSeconds();
    $("#date").val(y + "-" + mon + "-" + d + " " + h + ":" + m + ":" + s);
}

//获取产品的信息
function load() {
    let user=JSON.parse(localStorage.getItem("user"))
    $("#rname").val(user.rname);
    let BianMde=JSON.parse(localStorage.getItem("BianMde"))
    //设计单编号
    $("#designid").text(BianMde.designid);
    //设计人
    $("#designer").text(BianMde.designer);
    //产品编号
    $("#productid").text(BianMde.productid);
    //产品名称
    $("#productname").text(BianMde.productname);
    /*//总工时
    $("#costpricesum").text(BianMde.costpricesum);*/
    //设计要求
    $("#proceduredescribe").val(BianMde.proceduredescribe);

    $.post("/Mdesignproceduredetails.do/ChaById", {id:BianMde.id}, function (sdata) {
        let str = " <tr>\n" +
            "           <th>工序编号</th>\n" +
            "           <th>工序名称</th>\n" +
            "           <th>描述</th>\n" +
            "           <th>工时数</th>\n" +
            "           <th>工时单位</th>\n" +
            "           <th>单位工时成本</th>\n" +
            "           <th>工时完成小计(元)</th>\n" +
            "       </tr>\n";
        list=sdata;
        $.each(sdata, function (index, item) {
            str +=
                "  <tr>\n" +
                "      <td>" + item.detailsnumber + "</td>\n" +
                "      <td>" + item.procedurename + "</td>\n" +
                "      <td>" + item.proceduredescribe + "</td>\n" +
                "      <td><input type=\"number\" id='GS' value='"+item.labourhouramount+"' class='GongShi' style=\"border: none;height: 100%;width: 100%;\"></td>\n" +
                "      <td>h</td>\n" +
                "      <td><input type=\"number\" id='CB' value='"+item.costprice+"' class='ChengBen' style=\"border: none;height: 100%;width: 100%;\"></td>\n" +
                "      <td><input class='xiaoji' id='XJ' value='"+item.subtotal+"' readonly></td>\n" +
                "  </tr>";
        })
        $("#tableAdd").html(str);
    })
}

//预览
function yl() {

    $("#submit").removeAttr("hidden");
    let shu = [];
    $(".GongShi").each(function () {
        shu.push($(this).val());
    })
    let dj = [];
    $(".ChengBen").each(function () {
        dj.push($(this).val());
    })
    let s = 0;
    $(".xiaoji").each(function () {
        console.log(shu[s] * dj[s])
        $(this).val(parseInt(shu[s]) * parseInt(dj[s]))
        s = s + 1;
    })
}
//提交
function Submit() {
    let shu = [];
    let gh = 0;
    $(".GongShi").each(function () {
        shu.push($(this).val());
        gh += parseInt($(this).val());
    })
    for (let i = 0; i < shu.length; i++) {
        if (shu[i] == "") {
            $("#tishi").text("数量不能为小于等于0 ！")
            $("#tishi").css("color", "black")
            $("#staticBackdrop").modal("show");
            return;
        }
    }
    let dj = [];
    let cb = 0;
    $(".ChengBen").each(function () {
        dj.push($(this).val());
        cb += parseInt($(this).val());
    })
    let s = 0;
    let xj=0;
    let xiaoji=[];
    $(".xiaoji").each(function () {
        xiaoji.push($(this).val());
        xj +=shu[s] * dj[s];
        s = s + 1;
    })
    for (let i = 0; i < dj.length; i++) {
        if (parseInt(dj[i]) <= 0) {
            alert("工时不能为小于等于0！")
            return;
        }
    }
    let BianMde=JSON.parse(localStorage.getItem("BianMde"))
    let mdesignprocedure ={
        "id":BianMde.id,
        "costpricesum":xj,
        "changer":$("#rname").val(),
        "changetime":$("#date").val(),
        "checktag":"S001-0",
        "designmoduletag":"G002-0",
        "changetag":"B002-1",
        "designmodulechangetag":"G003-0"
    }
    let mdList=[];
    for (let i=0;i<list.length;i++){
        let mDesignProcedureDetails={
            "id":list[i].id,
            "labourhouramount":shu[i],
            "costprice":dj[i],
            "subtotal":xiaoji[i],
            "register":$("#rname").val(),
            "registertime":$("#date").val()
        }
        mdList[i]=mDesignProcedureDetails;
    }
    let md = JSON.stringify(mdesignprocedure);
    let MDList = JSON.stringify(mdList);

    $.ajax({
        type:"post",
        url:"/Mdesignproceduredetails.do/updateByMingXi",
        data:{mdesignprocedure:md,MDList:MDList},
        success:function (data) {
            location.href = "../../li_meng/SheJiDanBianGeng.html";
        }
    })


}